//
//  KLPhotoBroswerController.m
//  HealthManage
//
//  Created by 陈华 on 2017/4/10.
//  Copyright © 2017年 kinglian. All rights reserved.
//

#import "KLPhotoBroswerController.h"
#import "KLPhotoBroswerCell.h"
#import "GPMessageItem.h"
#import <UIImageView+WebCache.h>

@interface KLPhotoBroswerController () <UICollectionViewDelegate, UICollectionViewDataSource>

/**collectionView*/
@property(nonatomic,weak) UICollectionView *collectionView;

@end

@implementation KLPhotoBroswerController

static NSString *ID = @"photo";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置UI
    [self setupUI];
    
}

#pragma mark - 设置UI
/**设置UI*/
- (void)setupUI
{
    //初始化
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = self.view.bounds.size;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[KLPhotoBroswerCell class] forCellWithReuseIdentifier:ID];
    collectionView.pagingEnabled = YES;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    //滚动到指定的位置
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.collectionView.frame = self.view.bounds;
}



#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.messages.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KLPhotoBroswerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.message = self.messages[indexPath.row];
    
    cell.option = ^(){
        [self dismissViewControllerAnimated:YES completion:nil];
    };
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //求出当前的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    //当前的页码
    NSUInteger index = offsetX / scrollView.bounds.size.width;
    //记录
    self.selectedIndex = index;
}




#pragma mark - AnimatorDismissDelegate -- 动画相关
- (NSIndexPath *)indexPathForDismissView
{
    //取出当前的模型
    GPMessageItem *item = [self.messages objectAtIndex:self.selectedIndex];
    NSInteger index = [self.originMessages indexOfObject:item];
    return [NSIndexPath indexPathForRow:index inSection:0];
}

- (UIImageView *)imageViewForDimissView
{
    UIImageView *imageView = [[UIImageView alloc]init];
    
    //取出当前的模型
    GPMessageItem *message = self.messages[self.selectedIndex];
    
    //设置图片
    imageView.image = message.image;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    return imageView;
}




@end
































